package cn.tedu._05weibo.controller;

import cn.tedu._05weibo.base.response.JsonResult;
import cn.tedu._05weibo.base.response.StatusCode;
import cn.tedu._05weibo.mapper.UserMapper;
import cn.tedu._05weibo.pojo.dto.UserLoginDto;
import cn.tedu._05weibo.pojo.dto.UserRegDTO;
import cn.tedu._05weibo.pojo.entity.User;
import cn.tedu._05weibo.pojo.vo.UserVo;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import javax.validation.constraints.NotNull;
import java.util.Date;

/**
 * @Author Justin Parker
 * @Date 2024/8/9 0009 11:23
 * @Version 1.0
 */
@Api(tags = "01.用户模块")
@Slf4j
@RestController
@RequestMapping("/v1/users")
public class UserController {

    @Autowired(required = false)
    private UserMapper userMapper;

    /**
     * 1.注册功能
     *  1.1 RequestBody注解：请求体注解
     *  1.2 ResponseBody注解：响应体注解
     *
     */
    @ApiOperation(value = "注册功能")
    @PostMapping("reg")
    public JsonResult reg(@RequestBody @Validated UserRegDTO userRegDTO){
        /**
         * 1.接收客户端传递的数据
         * 2.校验数据的合法性【此处暂时不校验】
         * 3.确定用户名是否被占用【select接口】
         *  3.1 如果被占用，return 2
         *  3.2 如果没被占用，【insert接口】,return 1
         */
        System.out.println("userRegDTO = " + userRegDTO);
        String username = userRegDTO.getUsername();
        UserVo userVo = userMapper.selectByUsername(username);
        if (userVo != null){
            return new JsonResult(StatusCode.USERNAME_EXIST);
        }
        User user = new User();
        BeanUtils.copyProperties(userRegDTO,user);
        user.setCreated(new Date());
        userMapper.insert(user);
            return JsonResult.ok();
        }

        @ApiOperation(value = "登录功能")
        @PostMapping("login")
        public JsonResult login(@RequestBody UserLoginDto userLoginDto,@ApiIgnore HttpSession session){
            System.out.println("userLoginDto = " + userLoginDto);
            String username = userLoginDto.getUsername();
            String password = userLoginDto.getPassword();
            UserVo userVo = userMapper.selectByUsername(username);
            if (userVo == null){
                return new JsonResult(StatusCode.USERNAME_ERROR);
            }
            if (userVo.getPassword().equalsIgnoreCase(password)){
                //保存该用户的session会话到服务器中；
                session.setAttribute("user",userVo);
                return JsonResult.ok();
            }
                return new JsonResult(StatusCode.PASSWORD_ERROR);
        }

    /**
     * 3.获取当前用户功能
     */
    @ApiOperation(value = "获取当前用户功能")
    @GetMapping("currentUser")
    public JsonResult currentUser(@ApiIgnore HttpSession session){
        //在服务器的内存中，获取会话标识
    UserVo userVo =(UserVo) session.getAttribute("user");

    return JsonResult.ok(userVo);
    }

    /**
     * 4.退出功能
     */
    @ApiOperation(value = "退出功能")
    @GetMapping("logout")
    public void logout(@ApiIgnore HttpSession session){
        session.removeAttribute("user");

    }
}
